<?php
/**
 *  附加模块 ******************************************************************************************
 */
add_action('add_meta_boxes', 'additive_box');
function additive_box() {
	add_meta_box('additive_id', '附加模块', 'additive_textbox', 'post', 'side', 'default');
}

//~ 创建文本框
$additive = array( array('checked' => 'slide', 'title' => '设置为幻灯片1'));
function additive_textbox() {
	global $additive, $post;
	foreach ($additive as $meta_box) {
		echo '<p><label><input type="checkbox" name="' . $meta_box['checked'] . '" id="' . $meta_box['checked'] . '"' . (get_post_meta($post -> ID, $meta_box['checked'], true) ? ' checked="checked"' : '') . ' />' . $meta_box['title'] . '</label></p>';
	}
}

//~ 保存文章数据
add_action('save_post', 'additive_save');
function additive_save($post_id) {
	global $additive, $post;
	$post_type = get_post_type_object($post -> post_type);
	if (!current_user_can($post_type -> cap -> edit_post, $post_id))
		return $post_id;

	foreach ($additive as $meta_box) {
		$old = get_post_meta($post_id, $meta_box['checked'], true);
		$new = $_POST[$meta_box['checked']];
		if ($new && $new != $old) {
			update_post_meta($post_id, $meta_box['checked'], $new);
		} elseif ('' == $new && $old) {
			delete_post_meta($post_id, $meta_box['checked'], $old);
		}
	}
}

/**
 *  文章来源模块 ******************************************************************************************
 */
add_action('add_meta_boxes', 'source_box');
function source_box() {
	add_meta_box('source_id', '文章来源', 'source_textbox', 'post', 'normal', 'high');
}

//~ 创建文本框
$source = array( array("text" => "source_text", "title" => "文章来源网站名称："), array("text" => "source_url", "title" => "文章来源网站地址：（例如：http://www.itjc.cc/1234.html）"));
function source_textbox() {
	global $source, $post;
	foreach ($source as $meta_box) {
		echo '<p>' . $meta_box['title'] . '</p><input id="trackback_url" type="text" id="post_name" name="' . $meta_box['text'] . '"  value="' . get_post_meta($post -> ID, $meta_box['text'], true) . '"/>';
	}
}

//~ 保存文章数据
add_action('save_post', 'source_save');
function source_save($post_id) {
	global $source, $post;
	$post_type = get_post_type_object($post -> post_type);
	if (!current_user_can($post_type -> cap -> edit_post, $post_id))
		return $post_id;

	foreach ($source as $meta_box) {
		$old = get_post_meta($post_id, $meta_box['text'], true);
		$new = $_POST[$meta_box['text']];
		if ($new && $new != $old) {
			update_post_meta($post_id, $meta_box['text'], $new);
		} elseif ('' == $new && $old) {
			delete_post_meta($post_id, $meta_box['text'], $old);
		}
	}
}

//~ 读取文章来源名称
function get_post_source_text($post_id) {
	$count_key = 'source_text';
	$count = get_post_meta($post_id, $count_key, true);
	return $count;
}

//~ 读取文章来源地址
function get_post_source_url($post_id) {
	$count_key = 'source_url';
	$count = get_post_meta($post_id, $count_key, true);
	return $count;
}

/**
 *  下载模块 ******************************************************************************************
 */
add_action('add_meta_boxes', 'download_box');
function download_box() {
	add_meta_box('download_id', '下载', 'download_textbox', 'post', 'normal', 'high');
}

//~ 创建文本框
$download = array( array("text" => "download_text", "title" => "文件名称："), array("text" => "download_url", "title" => "文件链接：（例如：http://www.itjc.cc/1234.html）"));
function download_textbox() {
	global $download, $post;
	foreach ($download as $meta_box) {
		echo '<p>' . $meta_box['title'] . '</p><input id="trackback_url" type="text" id="post_name" name="' . $meta_box['text'] . '"  value="' . get_post_meta($post -> ID, $meta_box['text'], true) . '"/>';
	}
}

//~ 保存文章数据
add_action('save_post', 'download_save');
function download_save($post_id) {
	global $download, $post;
	$post_type = get_post_type_object($post -> post_type);
	if (!current_user_can($post_type -> cap -> edit_post, $post_id))
		return $post_id;
	foreach ($download as $meta_box) {
		$old = get_post_meta($post_id, $meta_box['text'], true);
		$new = $_POST[$meta_box['text']];
		if ($new && $new != $old) {
			update_post_meta($post_id, $meta_box['text'], $new);
		} elseif ('' == $new && $old) {
			delete_post_meta($post_id, $meta_box['text'], $old);
		}
	}
}

//~ 读取文件名
function get_post_download_text($post_id) {
	$count_key = 'download_text';
	$count = get_post_meta($post_id, $count_key, true);
	return $count;
}

//~ 读取文件链接
function get_post_download_url($post_id) {
	$count_key = 'download_url';
	$count = get_post_meta($post_id, $count_key, true);
	return $count;
}

/**
 *  文章浏览 ******************************************************************************************
 */
function set_post_views($post_id) {
	$count_key = 'views';
	$count = get_post_meta($post_id, $count_key, true);
	if ($count == '') {
		$count = 0;
		delete_post_meta($post_id, $count_key);
		add_post_meta($post_id, $count_key, '0');
	} else {
		$count++;
		update_post_meta($post_id, $count_key, $count);
	}
}

//~ 读取文章浏览量
function get_post_views($post_id) {
	$count_key = 'views';
	$count = get_post_meta($post_id, $count_key, true);
	if (empty($count)) {
		delete_post_meta($post_id, $count_key);
		add_post_meta($post_id, $count_key, '0');
		return '0';
	}
	return $count;
}

/**
 * 文章点赞计数 ******************************************************************************************
 */
add_action('wp_ajax_nopriv_like', 'set_post_like');
add_action('wp_ajax_like', 'set_post_like');
function set_post_like() {
	$id = $_POST["um_id"];
	$action = $_POST["um_action"];
	if ($action == 'like') {
		$bigfa_raters = get_post_meta($id, 'like', true);
		$expire = time() + 99999999;
		$domain = ($_SERVER['HTTP_HOST'] != 'localhost') ? $_SERVER['HTTP_HOST'] : false;
		setcookie('like_' . $id, $id, $expire, '/', $domain, false);
		if (!$bigfa_raters || !is_numeric($bigfa_raters)) {
			update_post_meta($id, 'like', 1);
		} else {
			update_post_meta($id, 'like', ($bigfa_raters + 1));
		}
		echo get_post_meta($id, 'like', true);
	}
	die ;
}

//~ 读取点赞数量
function get_post_like($post_id) {
	$count_key = 'like';
	$count = get_post_meta($post_id, $count_key, true);
	if (empty($count)) {
		delete_post_meta($post_id, $count_key);
		add_post_meta($post_id, $count_key, '0');
		return "0";
	}
	return $count;
}

/**
 * 文件下载计数 ******************************************************************************************
 */
add_action('wp_ajax_nopriv_download', 'set_post_download');
add_action('wp_ajax_download', 'set_post_download');
function set_post_download() {
	$id = $_POST["um_id"];
	$action = $_POST["um_action"];
	if ($action == 'download') {
		$bigfa_raters = get_post_meta($id, 'download', true);
		if (!$bigfa_raters || !is_numeric($bigfa_raters)) {
			update_post_meta($id, 'download', 1);
		} else {
			update_post_meta($id, 'download', ($bigfa_raters + 1));
		}
		echo get_post_meta($id, 'download', true);
	}
	die ;
}

//~ 读取文件下载数量
function get_post_download($post_id) {
	$count_key = 'download';
	$text_key = 'download_text';
	$count = get_post_meta($post_id, $count_key, true);
	$text = get_post_meta($post_id, $text_key, true);
	if (empty($text)) {
		if ($count == '0') {
			delete_post_meta($post_id, $count_key);
		}
	} elseif (empty($count)) {
		delete_post_meta($post_id, $count_key);
		add_post_meta($post_id, $count_key, '0');
		return "0";
	}
	return $count;
	return $text;
}

/**
 * 最新评论文章字段 ******************************************************************************************
 */
add_action('comment_post', 'comment_meta_update');
function comment_meta_update($comment_ID) {
	$comment = get_comment($comment_ID);
	$my_post_id = $comment -> comment_post_ID;
	update_post_meta($my_post_id, 'comment', time());
}

//~ 删除文章时删除评论字段
add_action('delete_post', 'comment_meta_delete');
function comment_meta_delete($post_ID) {
	if (!wp_is_post_revision($post_ID)) {
		delete_post_meta($post_ID, 'comment');
	}
}
